package com.koudai.finance.configuration;

import com.google.common.collect.Lists;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: WebConfigurer
 * @author: ls
 * @Date: 2020/7/16 10:07
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    @Resource
    private AuthInterceptor authInterceptor;

    /**
     * 这个方法是用来配置静态资源的，比如html，js，css，等等
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    }

    /**
     * @Description: 将StringHttpMessageConverter放的靠前一些 防止ResponseBodyAdvice处理String返回值报错
     * @author: ls
     * @Date: 2020/7/13 18:43
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(0, new MappingJackson2HttpMessageConverter());
    }

    /**
     * @Description: 自定义拦截器
     * @author: ls
     * @Date: 2020/7/13 18:43
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePathList = Lists.newArrayList();
        // 1、swagger放行url
        excludePathList.add("/doc.html");
        excludePathList.add("/service-worker.js");
        excludePathList.add("/swagger-resources/**");
        excludePathList.add("/swagger-ui.html");
        excludePathList.add("/configuration/ui");
        excludePathList.add("/configuration/security");
        excludePathList.add("/v2/api-docs");
        excludePathList.add("/error");
        excludePathList.add("/webjars/**");
        excludePathList.add("/**/favicon.ico");
        excludePathList.add("/**/favicon.ico");
        // 2、登录注册接口放行
        excludePathList.add("/login/**");
        // 3、支付回调放行
        excludePathList.add("/pay/wxPayCallBack");
        registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns(excludePathList);
    }
}
